package com.theBestBuy.code;

import java.net.BindException;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;

public class MyService extends Service{

	
	private Timer timer = new Timer();
	private static final long UPDATE_INTERVAL = 5000*60; //5 minutos
	private static IBinder mBinder = new Binder();
	private ArrayList<String> lista = new ArrayList<String>();
	private Titular[] datos;
	private int index =0;
	
	public void OnCreate(){
		super.onCreate();
		pollForUpdates();
	
	}

	private void pollForUpdates() {
		timer.scheduleAtFixedRate(new TimerTask() {			
			@Override
			public void run() {
				//webservice
				obtieneDatos("admin");
				
			}
		},0,UPDATE_INTERVAL);
		Log.i(getClass().getSimpleName(),"Timer started");
		
	}
	public Titular[] obtieneDatos(String usuario){
	datos = new Titular[]{
	new Titular("PSP barata", "Categoria: Electronica   Precio: 140 ","Propietario:admin   Telefono:618347835"),
	new Titular("PSP barata", "Categoria: Electronica   Precio: 140 ","Propietario:admin   Telefono:618347835"),
	new Titular("PSP barata", "Categoria: Electronica   Precio: 140 ","Propietario:admin   Telefono:618347835"),
	new Titular("PSP barata", "Categoria: Electronica   Precio: 140 ","Propietario:admin   Telefono:618347835"),
	new Titular("PSP barata", "Categoria: Electronica   Precio: 140 ","Propietario:admin   Telefono:618347835")};
	return datos;
	}

	public void onDestroy(){
		super.onDestroy();
		if(timer != null){
			timer.cancel();	
		}
		Log.i(getClass().getSimpleName(),"Timer stopped");
			
	}

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return mBinder;
	}
	public class MyBinder extends Binder{
		MyService getService(){
				
			return MyService.this;
		}
		
	}
	public Titular[] getDatos(){
		return datos;
		
	}

	
}
